
1#得到的数都是前两个数的和
fibs=[0,1]
for i in range(8):
	fibs.append(fibs[-2]+fibs[-1])
print fibs

2#加个参数
def method1(x):
	'hello word'
	return x*x
print method1(1)
print '-------------------------------------------'
3#
def test():
	print 'this is a dog.'
	return
	print 'this is a cat.'
	
x=test()  #this is a dog.
print x #None

#4.
names=['wangweiya','wangkun']
def change(n):
	n[0]='mili'
change(names)  #列表是可变的
print names    #['mili', 'wangkun']

new_name=names[:]
print new_name is names #False
print new_name==names   #True

new_name[0]='jack'     #可以改变new_name 的值，但是不会影响到names
print  new_name,names  #['jack', 'wangkun'] ['mili', 'wangkun']

change(new_name)
print new_name,names  #['mili', 'wangkun'] ['mili', 'wangkun']

def init(data):
	data['first']={}
	data['mid']={}
	data['last']={}
storage={}
init(storage)
print storage   #{'last': {}, 'mid': {}, 'first': {}}

#5改变参数的值
def inc(x):x[0]=x[0]+1
foo=[10]
inc(foo)
print foo

#6 参数的顺序

def method3(name,age):
	print '%s:%s' % (name,age)

method3(age=15,name='wangweiya') #关键字参数，更精确，顺序可以换

#7关键字参数最厉害的地方是可以给其使用默认值
def method4(name='wangkun',greeting='hello'):
	return '%s %s' % (greeting,name)
print method4()       # hello wangkun
print method4('wangweiya')  # hello wangweiya
print method4('mili','Hi')  # Hi mili

#如果想让问候语使用默认值，指定名字可以如下：
print method4(name='jack')

#8收集参数
def method5(*para):
	print para
method5(2)  # (2,)   默认使用元组数量为1时  ',' 打印出来了
method5(1,2,3,4)  # (1, 2, 3, 4)

def method6(name,*param):
	print name,param
method6('param:',1,2,3)  #  param: (1, 2, 3)

method6('param:')    # param: ()

#(**param)  返回字典
def method6(**param):
	print param
method6(x=1,y=2,z=3)  #{'y': 2, 'x': 1, 'z': 3}

def method7(x,y,z=1,*para,**param):
	print x,y,z  
	print para
	print param
	
method7(2,2,2,4,5,name='wangweiya',age=27)
#2 2 2     # (4, 5)  # {'age': 27, 'name': 'wangweiya'}

#9 参数收集的逆过程
def add(x,y):
	return x+y
parameter=(2,3)
print add(*parameter)  # 5

def method8(name1,age1):
	return name1,age1
params={'name1':'rose','age1':25}
print method8(**params)  # ('rose', 25)

def demo(**para):
	print para['name'],para['age']
	
def demo1(para):
	print para['name'],para['age']
	
para={'name':'james','age':24}
demo(**para) # james 24
demo1(para)  # james 24

parames=(2,)*2
print parames  #(2,2)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      


#如果使用全局变量时    globals()['param']  当参数和全局变量同名时全局变量会被屏蔽
def foo(param):
	print param+globals()['param']
param='weiya'
foo('wang')  # wangweiya
#函数内部的变量自动为局部变量，如果想把它设成全局变量做法如下：
z=1
def foo2():
	global z
	z=z+1
	print z
foo2()

#闭包
def add(x):
	def add_y(y):
		return x+y
	return add_y
temp=add(5)
print temp(10)  # 15

#递归     阶乘:(n! =n*(n-1)!)

def ck(n):
	result=n
	for i in range(1,n):
		result*=i
	return result
	
def ck1(n):
	if n==1 or n==0:
		return 1
	else:
		return n*ck1(n-1)

#幂
def mul(num,n):
	result=num
	for i in range(n-1):
		result*=num
	return result
def mul1(num,n):
	result=1
	for i in range(n):
		result*=num
	return result

def mul2(num,n):  #递归
	if n==0:
		return 1
	else:
		return num*mul2(num,n-1)

#幂
def mul(num,n):
	result=num
	for i in range(n-1):
		result*=num
	return result
def mul1(num,n):
	result=1
	for i in range(n):
		result*=num
	return result
#二分查找 返回下标

def search(seq,num):
	low=0
	high=len(seq)-1
	while low<=high:
		mid=(low+high)/2
		guess=seq[mid]
		if guess==num:
			return mid
		if guess>num:
			high=mid-1
		else:
			low=mid+1
	return None
	
"""
本章新的函数: 
map(func,seq)
filter(func,seq)
reduce(func,seq)
apply(func,)  
"""
		










